require 'file_manager'

class Menu < ActiveRecord::Base
  private
    def self.filter_list(request,list)
      list.delete_if do |x| 
        (not FileManager.can_execute?(request, x)) or
        ((x.url == '#') and (x.children(request).count == 0))
      end
    end
  
  public

  def children(request)
    Menu.filter_list(request, Menu.find_all_by_menu_id(id, :order => "\"order\" ASC"))
  end
  
  def self.find_roots(request)
    filter_list(request, find_all_by_menu_id(0, :order => "\"order\" ASC"))
  end

  def is_for_writer?
    /writer.*/ =~ url
  end
  
  def is_for_calc?
    /calc.*/ =~ url
  end

  def is_for_presentation?
    /impress.*/ =~ url
  end

end
